package com.zx.crowdfunding.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import com.zx.crowdfunding.entity.Auth;
import com.zx.crowdfunding.entity.AuthExample;
import com.zx.crowdfunding.entity.Role;
import com.zx.crowdfunding.entity.AuthExample.Criteria;
import com.zx.crowdfunding.global.CrowdFundingGlobal;
import com.zx.crowdfunding.mapper.AuthMapper;
import com.zx.crowdfunding.mapper.RoleMapper;
import com.zx.crowdfunding.service.api.AuthService;

/**
 * 权限 - Service接口实现类
 * @author 郑雪
 * @date 2021-12-09
 */
@Service
public class AuthServiceImpl implements AuthService {
	
	/**
	 * 权限Mapper接口
	 */
	@Autowired
	private AuthMapper authMapper;
	
	/**
	 * 角色Mapper接口
	 */
	@Autowired
	private RoleMapper roleMapper;

	@Override
	public List<Auth> getAll() {
		AuthExample example = new AuthExample();
		example.setOrderByClause("order_num ASC");
		return authMapper.selectByExample(example );
	}

	@Override
	public void saveAuth(Auth auth) {
		authMapper.insert(auth);
	}

	@Override
	public void removeAuth(List<Integer> idList) {
		AuthExample example = new AuthExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(idList);
		authMapper.deleteByExample(example);
	}

	@Override
	public Auth getAuthById(Integer id) {
		return authMapper.selectByPrimaryKey(id);
	}

	@Override
	public void editAuth(Auth auth) {
		authMapper.updateByPrimaryKeySelective(auth);
	}

	@Override
	public List<Auth> getRoleAssignedAuth(Integer roleId) {
		return authMapper.selectRoleAssignedAuth(roleId);
	}

	@Override
	public List<String> getAssignedAuthCodeByAdminId(Integer adminId) {
		return authMapper.selectAssignedAuthCodeByAdminId(adminId);
	}
	
	@Override
	public List<Auth> getMenuAssignedAuth(Integer menuId) {
		return authMapper.selectMenuAssignedAuth(menuId);
	}
	
	@Override
	public List<Auth> getOperationAssignedAuth(Integer operationId) {
		return authMapper.selectOperationAssignedAuth(operationId);
	}

	@Override
	public List<Map<String, Object>> getAllResourceAuth() {
		return authMapper.selectAllResourceAuth();
	}

	@Override
	public List<Map<String, Object>> getAllCodeAuth() {
		return authMapper.selectAllCodeAuth();
	}

	@Override
	public void updateGlobalCodeAuth(ServletContext servletContext) {
		
		// 查询代码权限集合
		List<Map<String,Object>> codeAuthList = authMapper.selectAllCodeAuth();
		
		// 更新全局静态变量和Application域中的值
		CrowdFundingGlobal.updateGlobalCodeAuth(codeAuthList, servletContext);
		
	}

	@Override
	public List<GrantedAuthority> getGrantedAuthorityListByAdminId(
			Integer adminId) {
		
		// 3.查询已分配角色
		List<Role> assignedRoleList = roleMapper.selectAssignedRole(adminId);
		
		// 4.查询已分配权限代码
		List<String> assignedAuthCodeList = authMapper.selectAssignedAuthCodeByAdminId(adminId);
		
		// 5.创建集合对象用来存储GrantedAuthority
		List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		
		// 6.遍历assignedRoleList存入角色信息
		for (Role role : assignedRoleList) {
			
			// 注意：不要忘了加前缀！
			String roleName = "ROLE_"+role.getName();
			
			SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleName);
			
			authorities.add(simpleGrantedAuthority);
		}
		
		// 7.遍历assignedAuthCodeList存入权限信息
		for (String authCode : assignedAuthCodeList) {
			
			SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(authCode);
			
			authorities.add(simpleGrantedAuthority);
		}
		
		// 8.返回结果
		return authorities;
	}

}
